<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="黑马头条">
      <template #left>
        <van-icon name="arrow-left" size="18" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!----------------- 图片预览 -------------------------->

    <!-- /导航栏 -->
    <div class="main-wrap">
      <!-- 加载中 -->
      <div class="loading-wrap" v-if="loading">
        <van-loading color="#3296fa" vertical>加载中</van-loading>
      </div>
      <!-- /加载中 -->
      <!-- 加载完成-文章详情 -->
      <div class="article-detail" v-else-if="articleInfo.title">
        <div>
          <img
            src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpg"
            style="width:100px"
            @click="picLook"
          />
        </div>
        <!-- 文章标题 -->
        <h1 class="article-title">{{articleInfo.title}}</h1>
        <!-- /文章标题 -->
        <!-- 用户信息 -->
        <van-cell class="user-info" center :border="false">
          <van-image
            class="avatar"
            slot="icon"
            round
            fit="cover"
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />
          <div slot="title" class="user-name">{{articleInfo.aut_name}}</div>
          <div slot="label" class="publish-date">{{articleInfo.pubdate|format}}</div>
          <!------------------ 关注按钮分离组件 ------------------------>
          <articleFoll :articleInfo="articleInfo" v-model="articleInfo.is_followed"></articleFoll>
        </van-cell>
        <!-- /用户信息 -->
        <!-- 文章内容 -->
        <div class="article-content" v-html="articleInfo.content" ref="article-content"></div>
        <van-divider>正文结束</van-divider>
        <!-- 插入评论列表组件 -->
        <article-comments
          :source="articleInfo.art_id"
          @total="totalCount=$event"
          :list="commentList"
          @replyBtn="replyBtn"
        ></article-comments>
      </div>
      <!-- /加载完成-文章详情 -->

      <!-- 加载失败：404 -->
      <div class="error-wrap" v-else-if="notfind">
        <van-icon name="failure" />
        <p class="text">该资源不存在或已删除！</p>
      </div>
      <!-- /加载失败：404 -->

      <!-- 加载失败：其它未知错误（例如网络原因或服务端异常） -->
      <div class="error-wrap" v-else>
        <van-icon name="failure" />
        <p class="text">内容加载失败！</p>
        <van-button class="retry-btn" @click="retryBtn">点击重试</van-button>
      </div>
      <!-- /加载失败：其它未知错误（例如网络原因或服务端异常） -->
    </div>

    <!-- 底部区域 -->
    <div class="article-bottom">
      <van-button class="comment-btn" type="default" round size="small" @click="commentBtrn">写评论</van-button>
      <van-icon name="comment-o" :info="totalCount" color="#777" />
      <!--------------------------- 收藏按钮分离组件 ---------------------------------------->
      <article-coll :articleInfo="articleInfo" @coll="articleInfo.is_collected=$event"></article-coll>
      <!-------------------------------- 收藏按钮分离组件 ----------------------------------------->
      <!-- <van-icon color="#777" name="good-job-o" /> -->
      <article-like :articleInfo="articleInfo" @att="articleInfo.attitude=$event"></article-like>
      <van-icon name="share" color="#777777"></van-icon>
    </div>
    <!-- /底部区域 -->
    <!-- 写评论  弹出层 -->
    <van-popup v-model="show" position="bottom" :style="{ height: '20%' }">
      <!--------------- 弹出层组件 -------------------->
      <article-text :target="articleInfo.art_id" @post-success="onpostSuccess" />
    </van-popup>
    <!--------- 回复弹出层 ---------------->
    <!-- 注意:弹出层是懒加载，首次点开弹出层，就会缓存里面的内容，再次关闭打开，
    只是显示隐藏，不贵更新侯麦难度数据相当一 v-show  利用呢v-if销毁dom元素，达到更新效果-->
    <van-popup v-model="replyShow" position="bottom" :style="{ height: '100%' }">
      <!-- 评论列表组件分离 -->
      <article-reply :item="item" @close="replyShow=false" v-if="replyShow"></article-reply>
      <!-- <article-item :item="item"></article-item> -->
    </van-popup>
  </div>
</template>
           
<script>
import { getAticles_api } from "@/api/search/index";
import { ImagePreview } from "vant";
import { setTimeout } from "timers";
import articleFoll from "@/components/article/article-followed";
import articleColl from "@/components/article/article-collected";
import articleLike from "@/components/article/article-like";
import articleComments from "@/components/article/article-comments";
import articleText from "@/components/article/article-comm-text";
import articleReply from "@/components/article/article-reply";
export default {
  components: {
    articleFoll,
    articleColl,
    articleLike,
    articleComments,
    articleText,
    articleReply
  },
  //依赖注入  给所有的后代组件传值 ，在子组件 inject 接收
    provide: function() {
      return {
        artId: this.article_id
      }
    },
  data() {
    return {
      article_id: this.$route.params.artId,
      articleInfo: [],
      loading: false, //页面加载状态
      notfind: false, //该资源不存在或已删除
      totalCount: 0, //评论数量
      show: false,
      commentList: [], //评论列表
      replyShow: false,
      item: []
    };
  },
  created() {
    this.getAticles();
    // console.log(this.article_id, "yyyyyyyyy");
    // console.log(this, 3434343);
  },
  methods: {
    //图片预览-------------------------
    picLook() {
      ImagePreview({
        images: [
          "https://img.yzcdn.cn/vant/apple-1.jpg",
          "https://img.yzcdn.cn/vant/apple-2.jpg",
          "https://img.yzcdn.cn/vant/apple-1.jpg"
        ],
        startPosition: 1
      });
    },
    //获取文章详细信息-------------------------
    async getAticles() {
      this.loading = true;
      // const jsonStr = this.article_id;
      // this.article_id = JSONBig.parse(jsonStr).toString();
      //  console.log(this,'ffffff')
      try {
        const { data: res } = await getAticles_api(this.article_id);
        console.log(res, 1111);
        this.articleInfo = res.data;
        //  console.log(this.articleInfo.is_followed,88888);
        //初始化图片点击预览，不能直接获取dom元素==>> 原因是在vue中数据去驱动视图不是立即的
        // console.log(this.$refs["article-content"]);
        //Vue中DOM的异步更新  $nextTick
        setTimeout(() => {
          //  console.log(this.$refs['article-content'])
          this.prviewImage();
        }, 0);
        this.$nextTick(() => {
          // console.log(this.$refs["article-content"]);
        });
      } catch (error) {
        // console.log(error.response.status, 2222);
        if (error.response.status == 404) {
          this.notfind = true;
        }
      }
      this.loading = false;
    },
    // 抽离点击关注按钮事件-------------------------------------------------
    // 抽离收藏按钮点击事件-------------------------------------------------
    //点击重试按钮,重新刷新页面
    retryBtn() {
      this.getAticles();
    },
    //遍历image的方法---------------------------------------------
    prviewImage() {
      //得到所有的img节点
      const articleContent = this.$refs["article-content"];
      const imgs = articleContent.querySelectorAll("img");
      const images = [];
      imgs.forEach((img, index) => {
        images.push(img.src);
        // console.log(images,'images')
        img.onclick = () => {
          //预览的图片地址数组
          ImagePreview({
            images,
            startPosition: index
          });
        };
      });
    },
    //写评论----------------------------------
    commentBtrn() {
      this.show = true;
    },
    //发布成功后，关闭弹出层,把发布内容显示到列表顶部
    onpostSuccess(data) {
      this.show = false;
      this.commentList.unshift(data.new_obj);
    },
    //点击回复按钮，显示弹出层
    replyBtn(item) {
      console.log(item, 55555);
      this.replyShow = true;
      this.item = item;
    }
  }
};
</script>
<style scoped lang="less">
.article-container {
  /deep/.page-nav-bar.van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  .main-wrap {
    position: fixed;
    left: 0;
    right: 0;
    top: 92px;
    bottom: 88px;
    overflow-y: scroll;
    background-color: #fff;
  }
  .article-detail {
    .article-title {
      font-size: 40px;
      padding: 50px 32px;
      margin: 0;
      color: #3a3a3a;
    }

    .user-info {
      padding: 0 32px;
      .avatar {
        width: 70px;
        height: 70px;
        margin-right: 17px;
      }
      .van-cell__label {
        margin-top: 0;
      }
      .user-name {
        font-size: 24px;
        color: #3a3a3a;
      }
      .publish-date {
        font-size: 23px;
        color: #b7b7b7;
      }
    }

    .article-content {
      padding: 55px 32px;
      /deep/ p {
        text-align: justify;
      }
    }
  }

  .loading-wrap {
    padding: 200px 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .error-wrap {
    padding: 200px 32px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    .van-icon {
      font-size: 122px;
      color: #b4b4b4;
    }
    .text {
      font-size: 30px;
      color: #666666;
      margin: 33px 0 46px;
    }
    .retry-btn {
      width: 280px;
      height: 70px;
      line-height: 70px;
      border: 1px solid #c3c3c3;
      font-size: 30px;
      color: #666666;
    }
  }

  .article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 88px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
    .comment-btn {
      width: 282px;
      height: 46px;
      border: 2px solid #eeeeee;
      font-size: 30px;
      line-height: 46px;
      color: #a7a7a7;
    }
    .van-icon {
      font-size: 40px;
      .van-info {
        font-size: 16px;
        background-color: #e22829;
      }
    }
  }
}
</style>